package com.ase.singleton.solution;

/**
 * @author gyhstart
 * @create 2021/3/6 - 23:03
 * 递归 1 + 2 +...+ 99 + 100
 **/
public class Solution3 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        System.out.println("sum(200) = " + sum(200));
        long endTime = System.currentTimeMillis();
        System.out.println("递归 = " + (endTime * 100000 - startTime * 100000));

        long startTime2 = System.currentTimeMillis();
        System.out.println("sum2(99) = " + sum2(50));
        long endTime2 = System.currentTimeMillis();
        System.out.println("for循环 = " + (endTime2 * 100000 - startTime2 * 100000));

    }

    public static int sum(int number) {
        if (number == 1) {
            return number;
        }
        return sum(number - 1) + number;
    }

    public static int sum2(int number) {
        int res = 0;
        for (int n = 1; n <= number; n++) {
            res += n;
        }
        return res;
    }
}
